package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
	"kop/pb"
	"kop/util/log"
	"time"
)

func main() {

	conn, err := grpc.Dial("127.0.0.1:8888", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	c := pb.NewConnectAPIServiceClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()

	ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{"token": getToken()}))
	resp, err := c.Connect(ctx, &wrappers.Int32Value{Value: 2})
	if err != nil {
		panic(err)
	}

	log.Println(*resp)

}

func getToken() string {

	conn, err := grpc.Dial("127.0.0.1:9999", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	c := pb.NewSignInAPIServiceClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	name := "xxxx111"
	r, err := c.SignIn(ctx, &pb.SignInRequest{UserName: name})
	if err != nil {
		panic(err)
	}

	fmt.Println(r.GetToken())
	return r.GetToken()
}
